package com.rey.demo.api;

import com.rey.demo.link.ZookeeperDemo;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

/**
 * @ClassName: DeleteApi
 * @Description: TODO
 * @Author reyJojo
 * @Date 2021/12/3 12:47
 * @Version 1.0
 */
public class DeleteApi {
    public static void main(String[] args) {
        try {
            ZooKeeper zooKeeper = ZookeeperDemo.link4ZookeeperApi();
            // 删除前先获取节点的数据结构
            assert zooKeeper != null;
            Stat stat = zooKeeper.exists("/seq_node0000000011", false);
            if(stat != null) {
                // 3.1.version = -1 是删除所有版本的节点
                zooKeeper.delete("/seq_node0000000011", stat.getVersion());
                // 3.2.删除一个含有子节点的节点（KeeperErrorCode = Directory not empty for /app）
                zooKeeper.delete("/app", -1);
            } else {
                System.out.println("节点不存在！");
            }
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
